package com.ucode.wap.hrm.controller;

import cn.hutool.core.util.ArrayUtil;
import com.ucode.hrm.service.HrmAttendanceApplyRecordService;
import com.ucode.hrm.service.HrmCustomerCompanyService;
import com.ucode.hrm.service.HrmCustomerProjectService;
import com.ucode.hrm.service.HrmEmployeeService;
import com.ucode.model.hrm.HrmAttendanceApplyRecord;
import com.ucode.model.hrm.HrmEmployee;
import com.ucode.model.hrm.convert.HrmAttendanceApplyRecordConvert;
import com.ucode.model.hrm.form.HrmAttendanceApplyRecordForm;
import com.ucode.ssm.user.service.AdminUserService;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.enums.CommonAuditStatusEnum;
import com.ucode.tool.util.IdentifyUtils;
import com.ucode.wap.hrm.service.ApiHrmEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "移动端 - 人力资源系统 - 补卡申请")
@RestController
@RequestMapping("/hrm/attendance-apply")
@Validated
public class ApiHrmAttendanceApplyRecordController {

    @Resource
    private HrmCustomerProjectService customerProjectService;
    @Resource
    private HrmCustomerCompanyService  customerCompanyService;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private HrmAttendanceApplyRecordService attendanceApplyRecordService;
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private ApiHrmEmployeeService apiHrmEmployeeService;

    @GetMapping("/page-list")
    @Operation(summary = "获得分页列表")
    public ResponseResult<Paginator<HrmAttendanceApplyRecord>> pageList(
            @Schema(description = "审批状态") @RequestParam(value = "auditStatus",required = false) Integer auditStatus,
            @Schema(description = "创建时间区间",example = "[2022-07-01 00:00:00,2022-07-01 23:59:59]")  @RequestParam(value = "createTimeInterval",required = false) LocalDateTime[] createTimeInterval,
            @Schema(description = "页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        Map<String,Object> condition = new HashMap<>();
        if(ArrayUtil.isNotEmpty(createTimeInterval) && createTimeInterval.length == 2){
            condition.put("startTime",createTimeInterval[0]);
            condition.put("endTime",createTimeInterval[1]);
        }
        HrmEmployee employee = apiHrmEmployeeService.verifyCurrentUserBindAndGetEmployee();
        condition.put("employeeId",employee.getId());
        condition.put("deltag",false);

        Paginator<HrmAttendanceApplyRecord> paginator = attendanceApplyRecordService.queryPageForList(condition,pageSize,pageNum);


        return ResponseResult.success(paginator);
    }

    @GetMapping("/work-date-list")
    @Operation(summary = "获得某一天的补卡记录列表")
    public ResponseResult<List<HrmAttendanceApplyRecord>> listByWorkDate(
            @Schema(description = "补卡时间") @RequestParam(value = "workDate",required = true) LocalDate workDate){

        Map<String,Object> condition = new HashMap<>();
        HrmEmployee employee = apiHrmEmployeeService.verifyCurrentUserBindAndGetEmployee();
        condition.put("employeeId",employee.getId());
        condition.put("workDate",workDate);
        condition.put("deltag",false);

        List<HrmAttendanceApplyRecord> list = attendanceApplyRecordService.queryObjectForList(condition,1);
        return ResponseResult.success(list);
    }

    @PostMapping("/create")
    @Operation(summary = "创建")
    public ResponseResult<Long> create(@Valid @RequestBody HrmAttendanceApplyRecordForm form) {

        HrmEmployee employee = apiHrmEmployeeService.verifyCurrentUserBindAndGetEmployee();
        HrmAttendanceApplyRecord bean = HrmAttendanceApplyRecordConvert.INSTANCE.convert(form);

        bean.setId(IdentifyUtils.getDefaultSnowflakeId());
        bean.setEmployeeId(employee.getId());
        bean.setProjectId(employee.getProjectId());
        bean.setCompanyId(employee.getCompanyId());
        bean.setAuditStatus(CommonAuditStatusEnum.SUBMIT_REVIEW.getCode());

        Long id = attendanceApplyRecordService.insert(bean);
        return id != null ? ResponseResult.success(id) : ResponseResult.failed();
    }

    @PostMapping("/logic-delete/{id}")
    @Operation(summary = "转移到回收站")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH, example = "1024")
    public ResponseResult<Object> logicDelete(@PathVariable(name="id",required=true) Long id) {
        //逻辑删除
        int i = attendanceApplyRecordService.logicDeletePrimaryKey(id);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }

    @PostMapping("/batch-logic-delete")
    @Operation(summary = "批量转移到回收站")
    public ResponseResult<Boolean> batchLogicDeletePrimaryKey(@Schema(description = "编号集合",requiredMode = Schema.RequiredMode.REQUIRED)
                                                @NotEmpty(message = "编号集合为空")@Size(min = 1,max = 100,message = "编号集合数量为1-100条")
                                                @RequestParam(value = "ids",required = true) List<Long> ids) {
        int i = attendanceApplyRecordService.logicDeleteByPrimaryKeys(ids);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }

}
